package com.ruoyi.custom.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.custom.domain.Custom;
import com.ruoyi.custom.service.ICustomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.custom.mapper.LoanMapper;
import com.ruoyi.custom.domain.Loan;
import com.ruoyi.custom.service.ILoanService;
import org.springframework.transaction.annotation.Transactional;

import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * 客户放款Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-25
 */
@Service
public class LoanServiceImpl extends ServiceImpl<LoanMapper, Loan> implements ILoanService
{
    @Autowired
    private LoanMapper loanMapper;
    @Autowired
    private ICustomService customService;

    /**
     * 查询客户放款
     *
     * @param id 客户放款主键
     * @return 客户放款
     */
    @Override
    public Loan selectLoanById(Long id)
    {
        return loanMapper.selectLoanById(id);
    }

    /**
     * 查询客户放款列表
     *
     * @param loan 客户放款
     * @return 客户放款
     */
    @Override
    public List<Loan> selectLoanList(Loan loan)
    {
        return loanMapper.selectLoanList(loan);
    }

    /**
     * 新增客户放款
     *
     * @param loan 客户放款
     * @return 结果
     */
    @Override
    @Transactional
    public int insertLoan(Loan loan)
    {
        loan.setCreateBy(getUsername());
        loan.setCreateTime(DateUtils.getNowDate());
        int result = loanMapper.insertLoan(loan);
        //修改客户表状态
        Custom custom = new Custom();
        custom.setId(Long.valueOf(loan.getCustomId()));
        custom.setStatus("2");
        customService.updateCustom(custom);
        return result;
    }

    /**
     * 修改客户放款
     *
     * @param loan 客户放款
     * @return 结果
     */
    @Override
    public int updateLoan(Loan loan)
    {
        return loanMapper.updateLoan(loan);
    }

    /**
     * 批量删除客户放款
     *
     * @param ids 需要删除的客户放款主键
     * @return 结果
     */
    @Override
    public int deleteLoanByIds(Long[] ids)
    {
        return loanMapper.deleteLoanByIds(ids);
    }

    /**
     * 删除客户放款信息
     *
     * @param id 客户放款主键
     * @return 结果
     */
    @Override
    public int deleteLoanById(Long id)
    {
        return loanMapper.deleteLoanById(id);
    }
}
